package video.hai.vui;

import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.content.Context;
import android.graphics.Typeface;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.text.Html;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
import video.hai.vui.R;
public class LazyAdapter extends BaseAdapter {
    
    private Activity activity;
    private ArrayList<HashMap<String, String>> data;
    private static LayoutInflater inflater=null;
    public ImageLoader imageLoader; 
    public int totalNumber=0;
    public SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
    public String currentDateandTime = sdf.format(new Date());
    public Context context;
    public Typeface typefaceTitle;
    public LazyAdapter(Activity a, ArrayList<HashMap<String, String>> d,Context context) {
        activity = a;
        data=d;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        imageLoader=new ImageLoader(activity.getApplicationContext());
        //Log.e("LazyAdapter", "ok2");
        this.context=context;
        typefaceTitle=Typeface.createFromAsset(context.getAssets(),"fonts/Roboto-Bold.ttf");
    }
// day se la so luong hien thi len listview
    public int getCount() {
    	try{
    		return data.size();
    	}catch(Exception ex)
    	{
    		return 0;
    	}
   }

    // lay 1 item tu vi tri cua no
    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }
    
    // day la noi gan len listview
    public View getView(int position, View convertView, ViewGroup parent) {
    	totalNumber++;
        View vi=convertView;
        if(convertView==null)
            vi = inflater.inflate(R.layout.list_row, null);

        TextView title = (TextView)vi.findViewById(R.id.title); // title
        ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image); // thumb image
        
        HashMap<String, String> news = new HashMap<String, String>();
        news = data.get(position);
        title.setTypeface(typefaceTitle);
        // Setting all values in listview
        title.setText(Html.fromHtml(news.get("title").trim()));       
        
        imageLoader.DisplayImage(news.get("image"), thumb_image);
        //Log.e("getView", "ok2");
        return vi;
    }
    public int totalHourDiff(String startTime,String endTime){    	
    	int hours=0;
    	long diff=0;
    	try{
	    	Date sTime = sdf.parse(startTime);
	    	Date eTime = sdf.parse(endTime);
	    	
	    	diff = eTime.getTime() - sTime.getTime();
	    	Log.e("totalHourDiff:diff=",String.valueOf(diff));
    	}catch(Exception ex){
    		
    	}
    	hours = (int)(diff/(60*60*1000));
    	return Math.abs(hours);
    }
    public int totalMinuteDiff(String startTime,String endTime){    	
    	int Minute=0;
    	long diff=0;
    	try{
	    	Date sTime = sdf.parse(startTime);
	    	Date eTime = sdf.parse(endTime);
	    	
	    	diff = eTime.getTime() - sTime.getTime();
	    	Log.e("totalHourDiff:diff=",String.valueOf(diff));
    	}catch(Exception ex){
    		
    	}
    	Minute = (int)(diff/(60*1000));
    	return Math.abs(Minute);
    }
}